﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Media3D;
using Microsoft.Research.Kinect.Nui;

namespace Wpf3dCollaborative
{
    /// <summary>
    /// Logique d'interaction pour Menu3d.xaml
    /// </summary>
    public partial class Menu3D : Canvas
    {

        private Boolean isMinimised = false;// Indique si le menu est replié
     
        public Menu3D()
        {
            
            InitializeComponent();
            Replier();
        }

        


        private void btnDemo_Click(object sender, RoutedEventArgs e)
        {
            for (int i = App.windows.mainViewport.Children.Count - 1; i >= 0; i--)
            {

                if (App.windows.mainViewport.Children[i] is SalleReunion)
                {
                    ((SalleReunion)App.windows.mainViewport.Children[i]).Demo();
                }
            }
        }


        private void btnAnimStop(object sender, RoutedEventArgs e)
        {
            for (int i = App.windows.mainViewport.Children.Count - 1; i >= 0; i--)
            {

                if (App.windows.mainViewport.Children[i] is SalleReunion)
                {
                    ((SalleReunion)App.windows.mainViewport.Children[i]).Transformer.StopAnim();
                }
            }

        }



        private void btnSoucoupe(object sender, RoutedEventArgs e)
        {

            for (int i = App.windows.mainViewport.Children.Count - 1; i >= 0; i--)
            {

                if (App.windows.mainViewport.Children[i] is SalleReunion)
                {
                    ((SalleReunion)App.windows.mainViewport.Children[i]).Transformer.SoucoupeVolante(Convert.ToInt32(txtTimeAnnim.Text));
                }
            }
        }

        private void btnRotation(object sender, RoutedEventArgs e)
        {

            for (int i = App.windows.mainViewport.Children.Count - 1; i >= 0; i--)
            {

                if (App.windows.mainViewport.Children[i] is SalleReunion)
                {
                    ((SalleReunion)App.windows.mainViewport.Children[i]).Transformer.AnnimCaroussel(Convert.ToInt32(txtTimeAnnim.Text));
                }
            }
        }

        private void ComboBoxItem_Selected(object sender, RoutedEventArgs e)
        {
            try
            {
                for (int i = App.windows.mainViewport.Children.Count - 1; i >= 0; i--)
                {

                    if (App.windows.mainViewport.Children[i] is SalleReunion)
                    {
                        ((SalleReunion)App.windows.mainViewport.Children[i]).IsCaroussel = false;
                        ((SalleReunion)App.windows.mainViewport.Children[i]).ClearAllTransforms();
                        ((SalleReunion)App.windows.mainViewport.Children[i]).RecalculateGrille();
                   
                    }
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine("[EX normale]" + ex.Message);

            }
        }

        private void ComboBoxItem_Selected_1(object sender, RoutedEventArgs e)
        {

            for (int i = App.windows.mainViewport.Children.Count - 1; i >= 0; i--)
            {

                if (App.windows.mainViewport.Children[i] is SalleReunion)
                {
                    ((SalleReunion)App.windows.mainViewport.Children[i]).ClearAllTransforms();
                    ((SalleReunion)App.windows.mainViewport.Children[i]).RecalculateCaroussel();
                    ((SalleReunion)App.windows.mainViewport.Children[i]).IsCaroussel = true;
                   
                }
            }

        }

        private void ComboBoxItem_Selected_2(object sender, RoutedEventArgs e)
        {

            for (int i = App.windows.mainViewport.Children.Count - 1; i >= 0; i--)
            {

                if (App.windows.mainViewport.Children[i] is SalleReunion)
                {
                    ((SalleReunion)App.windows.mainViewport.Children[i]).ClearAllTransforms();
                    ((SalleReunion)App.windows.mainViewport.Children[i]).RecalculateCaroussel();
                    ((SalleReunion)App.windows.mainViewport.Children[i]).RecalculateGrille();
                    ((SalleReunion)App.windows.mainViewport.Children[i]).IsCaroussel = true;
                }
            }
        }


       
        private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                for (int i = App.windows.mainViewport.Children.Count - 1; i >= 0; i--)
                {

                    if (App.windows.mainViewport.Children[i] is SalleReunion)
                    {
                        int res =  Convert.ToInt32(txtTimeAnnim.Text);
                        if (res <1 || res > 50000)
                        {
                            res = 2000;
                            txtTimeAnnim.Text = res.ToString();
                        }
                         

                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("[EX normale]" + ex.Message);

            }


        }

    

        private void UserControl_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Replier();
        }

        private void UserControl_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Replier();
        }

        public void Replier()
        {
            if (!isMinimised)
            {
                isMinimised = true;

                foreach (UIElement i in this.Children)
                {
                    i.Visibility = Visibility.Hidden;
                }
                this.Width = 5;
             
            }
            else
            {
                isMinimised = false;
                foreach (UIElement i in this.Children)
                {
                    i.Visibility = Visibility.Visible;
                }
                this.Width = 280;
            
            }
        }

        private void ToggleButton_Checked(object sender, RoutedEventArgs e)
        {
            try
            {
                App.windows.menukinect.KinectDiagno.Kinect = Runtime.Kinects[0];
                App.windows.menukinect.Visibility = Visibility.Visible;
            }
            catch(Exception Ex)
            {
                MessageBox.Show("Aucune Kinect detectée / Ou pilote non installé");
                btnKinect.IsChecked = false;
                Console.WriteLine("[Ex normale]:"+Ex.Source);
            }
        }

        private void ToggleButton_Unchecked(object sender, RoutedEventArgs e)
        {
            App.windows.menukinect.KinectDiagno.Kinect = null;
            App.windows.menukinect.Visibility = Visibility.Collapsed;
        }

        private void btnCommand_Click(object sender, RoutedEventArgs e)
        {

            if (App.windows.mainViewport.ShowCommandPanel)
            {
                App.windows.mainViewport.ShowCommandPanel = false;
            }
            else
            {
                App.windows.mainViewport.ShowCommandPanel = true;
            }

        }

        private void btnAjouterCam_Click(object sender, RoutedEventArgs e)
        {
            for (int i = App.windows.mainViewport.Children.Count - 1; i >= 0; i--)
            {

                if (App.windows.mainViewport.Children[i] is SalleReunion)
                {
                    ((SalleReunion)App.windows.mainViewport.Children[i]).AjouterCam();
                }
            }

        }


        private void btnCoeur_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                for (int i = App.windows.mainViewport.Children.Count - 1; i >= 0; i--)
                {

                    if (App.windows.mainViewport.Children[i] is SalleReunion)
                    {
                        ((SalleReunion)App.windows.mainViewport.Children[i]).AjouterCoeur();

                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("[EX normale]" + ex.Message);
            }
        }

        private void btnSquelette_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                for (int i = App.windows.mainViewport.Children.Count - 1; i >= 0; i--)
                {

                    if (App.windows.mainViewport.Children[i] is SalleReunion)
                    {
                        ((SalleReunion)App.windows.mainViewport.Children[i]).AjouterSquelette();

                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("[EX normale]" + ex.Message);
            }
        }

   

      

        private void btnFps_Checked(object sender, RoutedEventArgs e)
        {

            App.windows.ShowFPS();

        }


        private void button4_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                for (int i = App.windows.mainViewport.Children.Count - 1; i >= 0; i--)
                {

                    if (App.windows.mainViewport.Children[i] is SalleReunion)
                    {
                        ((SalleReunion)App.windows.mainViewport.Children[i]).Dispose();

                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("[EX normale]" + ex.Message);
            }
        }

      
       


    }
}
